package com.ftx.controller.test.annotation.beans;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * Created by fengtingxin on 2020/4/20.
 *
 * BeanPostProcessor执行原理：
 *  populateBean(beanName, mbd, instanceWrapper); 为bean的一些属性赋值
 *  进入  initializeBean(beanName, exposedObject, mbd) 方法
 *  {
 *      1.applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); 先执行postProcessBeforeInitialization
 *      执行方式为：循环遍历所有的BeanPostProcessor的 postProcessBeforeInitialization方法
 *
 *      2.invokeInitMethods 执行自定义初始化，这里的自定义初始化包括：
 *          1).实现InitializingBean
 *          2).使用@PostConstruct 注解
 *      3.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); 遍历beanPostProcessor的该方法。
 *  }
 *
 *  Spring底层对beanPostProcessor 的使用：
 *      bean的赋值、注入其他的组件、@AutoWired、生命周期的注解功能，@Async 、xxxBeanPostProcessor
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("MyBeanPostProcessor 。。。postProcessBeforeInitialization ");
        System.out.println("MyBeanPostProcessor 。。。postProcessBeforeInitialization "  + beanName + "==> " +bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//        System.out.println("MyBeanPostProcessor 。。。postProcessAfterInitialization "  + beanName + "==> " +bean);
        return bean;
    }
}
